package com.kjlink.internal.report4vue.plan.controller;

import cn.xuqiudong.common.base.model.BaseResponse;
import cn.xuqiudong.common.base.model.PageInfo;
import com.kjlink.internal.report4vue.base.controller.BaseApiController;
import com.kjlink.internal.report4vue.plan.lookup.PlanLookup;
import com.kjlink.internal.report4vue.plan.model.Plan;
import com.kjlink.internal.report4vue.plan.service.PlanService;
import com.kjlink.internal.workreport.helper.SessionHelper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 描述:
 * 计划管理 controller
 *
 * @author Vic.xu
 * @since 2025-03-17 13:54
 */
@RestController
public class ApiPlanController extends BaseApiController {

    @Resource
    private PlanService service;

    /**
     * 列表页
     */
    @RequestMapping(value = "/plan/list", method = {RequestMethod.GET, RequestMethod.POST})
    public BaseResponse<PageInfo<Plan>> list(@RequestBody PlanLookup lookup) {
        PageInfo<Plan> pager = service.page(lookup);
        return BaseResponse.success(pager);
    }


    /**
     * 删除
     */
    @PostMapping("/plan/delete/{id}")
    public BaseResponse<Integer> delete(@PathVariable Integer id) {
        int num = service.delete(id);
        return BaseResponse.success(num);
    }

    /**
     * 批量异步json新增
     */
    @PostMapping("/plan/batchInsert")
    public BaseResponse<?> batchInsert(@RequestBody List<Plan> list) {
        if (CollectionUtils.isNotEmpty(list)) {
            Integer userId = SessionHelper.currentUserId();
            list.forEach(plan -> {
                plan.setUserId(userId);
                service.save(plan);
            });
        }
        return BaseResponse.success();
    }

    /**
     * 更新
     */
    @PostMapping("/plan/update")
    public BaseResponse<Integer> update(@RequestBody Plan plan) {
        int num = service.update(plan);
        return BaseResponse.success(num);
    }

}
